package cn.tedu.egweibo5.controller;

import cn.tedu.egweibo5.base.response.JsonResult;
import cn.tedu.egweibo5.mappers.WeiboMapper;
import cn.tedu.egweibo5.pojo.dto.WeiboDTO;
import cn.tedu.egweibo5.pojo.entity.Weibo;
import cn.tedu.egweibo5.pojo.vo.UserVO;
import cn.tedu.egweibo5.pojo.vo.WeiboDetailVO;
import cn.tedu.egweibo5.pojo.vo.WeiboListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author: Miyako
 * @CreateTime: 2024-08-14  22:32
 * @Description: TODO
 */
@Slf4j
@Api(tags = "微博模块")
@Validated
@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired
    WeiboMapper weiboMapper;

    @ApiOperation("发布微博")
    @PostMapping("insert")
    public JsonResult insert(@Validated @RequestBody WeiboDTO weiboDTO , @ApiIgnore HttpSession session){
        UserVO user = (UserVO) session.getAttribute("user");
        Weibo weibo = new Weibo();
        weibo.setUserId(user.getId());
        log.debug("weibo"+weibo);
        weiboMapper.insert(weibo);
        return JsonResult.ok(null);
    }

    @ApiOperation("微博列表")
    @GetMapping("selectIndex")
    public JsonResult selectIndex (){
        List<WeiboListVO> weiboListVOS = weiboMapper.selectIndex();
        log.debug("weiboListVOS "+weiboListVOS);
        return JsonResult.ok(weiboListVOS);
    }

    @ApiOperation("微博详情")
    @GetMapping("selectById")
    @ApiImplicitParam(name = "id",value = "微博id",required = true,paramType = "long")
    public JsonResult selectById(@Range(min = 200 , max = 300 ,message = "微博编号最小200，最大300")@RequestParam Long id){
        WeiboDetailVO weiboDetailVO = weiboMapper.selectById(id);
        return JsonResult.ok(weiboDetailVO);
    }



}
